package com.taxi.management.controller;

import com.taxi.management.entity.ResultEntity;
import com.taxi.management.entity.UserEntity;
import com.taxi.management.entity.dto.UserLoginRequest;
import com.taxi.management.login.UserUtils;
import com.taxi.management.service.Impl.TokenServiceImpl;
import com.taxi.management.service.Impl.UserServiceImpl;
import com.taxi.management.vo.UserVo;
import jakarta.annotation.Resource;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("v3/user/")
@CrossOrigin//解决跨域问题
@RestController//Json
@Slf4j
public class UserController {
    @Resource
    UserServiceImpl userService;

    @Resource
    TokenServiceImpl tokenService;

    @GetMapping("getAllUser")
    public ResultEntity getAllUser() {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(userService.getAllUser());
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
            resultEntity.setCount(userService.getAllUser().size());
        } catch (Exception e) {
            e.printStackTrace();
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    @PostMapping("doLogin")
    public ResultEntity doLogin(@RequestBody UserLoginRequest userLoginRequest) {
        String username = userLoginRequest.getUsername();
        String password = userLoginRequest.getPassword();
        ResultEntity resultEntity = new ResultEntity();
        if (username == null || password == null) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("用户名或密码不能为空");
        } else {
            UserEntity user = userService.doLogin(username, password);
            if (user != null) {
                String uuid = UserUtils.save(user);
                resultEntity.setData(uuid);
                resultEntity.setCode(0);
                resultEntity.setMsg("登录成功！");
            } else {
                resultEntity.setCode(-1);
                resultEntity.setMsg("登录失败，无法获取Token！");
            }
        }
        return resultEntity;
    }

    @GetMapping("getUserById")
    public ResultEntity getUserById(@RequestParam int id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(userService.getUserById(id));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
            resultEntity.setCount(1);
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    @PostMapping("addUser")
    public ResultEntity addUser(@RequestBody UserEntity user) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(userService.addUser(user));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据添加成功！");
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
            e.printStackTrace();
        }
        return resultEntity;
    }

    @PutMapping("updateUser")
    public ResultEntity updateUser(@RequestBody UserEntity user) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(userService.updateUser(user));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据更新成功！");
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
            e.printStackTrace();
        }
        return resultEntity;
    }

    @DeleteMapping("deleteUser/{id}")
    public ResultEntity deleteUser(@PathVariable int id) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(userService.deleteUser(id));
            resultEntity.setCode(0);
            resultEntity.setMsg("数据删除成功！");
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    @GetMapping("getUserByPage")
    public ResultEntity getUserByPage(@RequestParam String username, @RequestParam int page, @RequestParam int limit) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            int start = (page - 1) * limit;
            int cnt = -1;
            List<UserEntity> list = userService.getUserByPage(username, start, limit);
            if (!username.equals("")) {
                cnt = list.size();//有查询条件的时候，就按查询回来的条数计
            } else {
                cnt = userService.getAllUser().size();//当没有查询条件的时候 就获取所有用户数量
            }
            resultEntity.setData(list);
            resultEntity.setCode(0);
            resultEntity.setMsg("数据获取成功！");
            resultEntity.setCount(cnt);
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }

    @GetMapping("getIdByToken")
    public ResultEntity getIdByToken(@RequestParam String token) {
        ResultEntity resultEntity = new ResultEntity();
        try {
            UserEntity user = UserUtils.get(token);
            if (user != null) {
                resultEntity.setData(user.getId());
                resultEntity.setCode(0);
                resultEntity.setMsg("数据获取成功！");
            } else {
                resultEntity.setCode(-1);
                resultEntity.setMsg("token无效！");
            }
        } catch (Exception e) {
            resultEntity.setCode(-1);
            resultEntity.setMsg("系统错误");
        }
        return resultEntity;
    }
}